package com.nega.commons.util;

import org.apache.commons.lang3.StringUtils;

import java.util.Random;

/**
 * @Author: Garcia
 * @CreateDate: 2019/5/17 15:52
 * @Description: what it is class?
 */
public class DataUtil {

    public static Double getRandomDecimal(Double min,Double max){
        int scl =  2;//小数最大位数
        int pow = (int) Math.pow(10, scl);//指定小数位
        return Math.floor((Math.random() * (max - min) + min) * pow) / pow;
    }

    public static int getRandomInteger(int min,int max){
        return (int)(min+Math.random()*(max-min+1));
    }

    public static String getRandomData(int length){
        StringBuilder sb=new StringBuilder();
        Random rand=new Random();//随机用以下三个随机生成器
        Random randdata=new Random();
        int data=0;
        for(int i=0;i<5;i++)
        {
            int index=rand.nextInt(3);
            //目的是随机选择生成数字，大小写字母
            switch(index)
            {
                case 0:
                    data=randdata.nextInt(10);//仅仅会生成0~9
                    sb.append(data);
                    break;
                case 1:
                    data=randdata.nextInt(26)+65;//保证只会产生65~90之间的整数
                    sb.append((char)data);
                    break;
                case 2:
                    data=randdata.nextInt(26)+97;//保证只会产生97~122之间的整数
                    sb.append((char)data);
                    break;
            }
        }
        return sb.toString().toUpperCase();
    }


    public static String hiddenData(String data){

        if (StringUtils.isBlank(data)){
            return data;
        }
        String value = "";
        if (data.length()<=7){
            value=data.substring(0, 1) + "********************" + data.substring(data.length()-1, data.length());
        }else {
            value=data.substring(0, 3) + "********************" + data.substring(data.length()-4, data.length());
        }

        return value;
    }
}
